package com.jianyun.jyzs.widget.badge;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;


public class HuaweiBadgeHandler implements IBadgeHandler {

    private static final String TAG = HuaweiBadgeHandler.class.getSimpleName();

    private Context context;
    boolean isSupportedBade = false;


    public HuaweiBadgeHandler(Context context) {
        this.context = context;
    }

    @Override
    public void updateBadgeCount(int count) {
        checkIsSupportedByVersion();
        if (isSupportedBade) {
            try {
                String launchClassName = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent().getClassName(); //启动的Activity完整名称
                Bundle bundle = new Bundle();//需要存储的数据
                bundle.putString("package", context.getPackageName());//包名
                bundle.putString("class", launchClassName);
                bundle.putInt("badgenumber", count);//未读信息条数
                context.getContentResolver().call(
                        Uri.parse("content://com.huawei.android.launcher.settings/badge/"),
                        "change_badge", null, bundle);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    public void checkIsSupportedByVersion() {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo("com.huawei.android.launcher",
                    0);
            if (info.versionCode >= 63025) {
                isSupportedBade = true;
            }
            Log.d(TAG, "checkIsSupportedByVersion versionCode " + info.versionCode + " isSupportedBade: " + isSupportedBade);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
